// pages/Brand/Brand.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
      'https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640',
      'https://images.unsplash.com/photo-1551446591-142875a901a1?w=640'
    ],
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000,


    // 首页轮播图
    pinpaiguang: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    display_multiple_items:3,
    active_index: 2,
    activ_name: '',
    array_area: [
      { id: 1, name: '全部' },
      { id: 2, name: '市级' },
      { id: 3, name: '增城区' },
      { id: 4, name: '从化区' }
    ],
    array_type: [
      { id: 1, name: '全部' },
      { id: 2, name: '巡防' },
      { id: 3, name: '信息' },
      { id: 4, name: '宣传' },
      { id: 5, name: '调解' },
      { id: 6, name: '其他' }
    ],
    array_status: [
      { id: '1', name: '全部' },
      { id: '2', name: '未开始' },
      { id: '3', name: '进行中' },
    ],
    index_area: 0,
    index_type: 0,
    index_status: 0,
    page:1,
    page1:1,
    allPage:'',
    allPage1: '',
    groupList:[],
    listImg:[]
  },

  tabItemShow: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.id;
    console.log(index);
    that.setData({
      active_index: index
    });
  },
  toActivityDetail: function () {
    wx.navigateTo({
      url: '/pages/ActivityDetail/ActivityDetail'
    })
  },

  bindPickerChangeArea: function (e) {
    let that = this;
    let index = e.detail.value;
    let array_area_id = that.data.array_area[index].id;
    console.log(array_area_id);
  },

  bindPickerChangeType: function (e) {
    let that = this;
    let index = e.detail.value;
    let array_type_id = that.data.array_type[index].id;
    console.log(array_type_id);
  },

  bindPickerChangeStatus: function (e) {
    let that = this;
    let index = e.detail.value;
    let array_status_id = that.data.array_status[index].id;
    console.log(array_status_id);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let index = options.id;
    this.setData({
      active_index: index
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (type) {
    var that=this;
    if (type == 1) {
    } else {
      that.setData({
        page: 1,
        page1:1
      })
    }
    var data = {
      paraUrl: "group/page?brand=false&page=" + that.data.page + "&size=5",
      paraData: ''
    }
    app.getRequest(data, function (res) {
      if (res.data.result == 200) {
        if (type==1){
          var groupList = that.data.groupList.concat(res.data.body.content)
        }else{
          var groupList = res.data.body.content
        }
        that.setData({
          groupList: groupList ,
          allPage: res.data.body.totalPages
        })
      } else {
      }
    })
    var data2 = {
      paraUrl: "group/page?brand=true&page=" + that.data.page1 + "&size=5",
      paraData: ''
    }
    app.getRequest(data2, function (res) {
      if (res.data.result == 200) {
        if (type == 1) {
          var pinpaiList = that.data.pinpaiList.concat(res.data.body.content)
        } else {
          var pinpaiList = res.data.body.content
        }
        that.setData({
          pinpaiList: pinpaiList,
          allPage1: res.data.body.totalPages
        })
      } else {
      }
    })
    var data1 = {
      paraUrl: 'advertisement/list?position=groups&type=recommended',
      paraData: ''
    }
    app.getRequest(data1, function (res) {
      if (res.data.result == 200) {
        that.setData({
          listImg: res.data.body
        });
      }
    })
    var data3 = {
      paraUrl: 'advertisement/list?position=brands&type=recommended',
      paraData: ''
    }
    app.getRequest(data3, function (res) {
      if (res.data.result == 200) {
        that.setData({
          pinpaiguang: res.data.body
        });
      }
    })
    // 是否点赞support
    // if (that.data.huodongDetail.support) {
    //   that.setData({
    //     support: true
    //   })
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    var that=this;
    if (that.data.page < that.data.allPage){
      that.setData({
        page: that.data.page+1,
        page1: that.data.page1 + 1
      })
      that.onShow(1);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 点击发起群组
  buildGroup: function (e) {
    wx.setStorageSync("formwhere",e.currentTarget.id)
    wx.navigateTo({
      url: '../CreateGroup/CreateGroup'
    })
  },
  // 点击我的群组
  // weGroup: function (e) {
  //   wx.setStorageSync('activorgroup', 2)
  //   wx.navigateTo({
  //     url: '../GroupAudit/GroupAudit'
  //   })
  // },
  // 点击查看群组详情
  clickDetail: function (e) {
    wx.setStorageSync('qundetail', e.currentTarget.id);
    wx.navigateTo({
      url: '../GroupDetail/GroupDetail'
    })
  },
  dianzan:function(e){
    var that=this;
    var data = {
      paraUrl: 'interaction/create',
      paraData: {
        "type": "support", 
        "interactionTarget": "group",
        "target": { 
          "id": e.currentTarget.dataset.type.id ,
          "code": e.currentTarget.dataset.type.code,
          "name": e.currentTarget.dataset.type.name
        }
      }
    }
    app.postRequest(data, function (res) {
      if (res.data.result == 200) {
        wx.showToast({
          title: '点赞成功',
          icon: 'success',
          duration: 2000
        })
        that.onShow('zan')
      } else if (res.data.result == 60013){
        wx.showToast({
          title: '不能重复点赞',
          icon: 'success',
          image: '/images/shibai.png',
          duration: 2000
        })
      }
    })
  },
  dianzanpinpai: function (e) {
    var that = this;
    var data = {
      paraUrl: 'interaction/create',
      paraData: {
        "type": "support",
        "interactionTarget": "brand",
        "target": {
          "id": e.currentTarget.dataset.type.object.id,
          "code": e.currentTarget.dataset.type.code,
          "name": e.currentTarget.dataset.type.name
        }
      }
    }
    app.postRequest(data, function (res) {
      if (res.data.result == 200) {
        wx.showToast({
          title: '点赞成功',
          icon: 'success',
          duration: 2000
        })
        that.onShow('zan')
      } else if (res.data.result == 60013) {
        wx.showToast({
          title: '不能重复点赞',
          icon: 'success',
          image: '/images/shibai.png',
          duration: 2000
        })
      }
    })
  },
  aa(){

  }
})